package com.seaboxdata.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

public class zhuijia{

    // 固定写法：国家统计局的首页链接
    private static final String link = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/";


    public static void main(String[] args) throws IOException {

        // 爬取省份
        Document document = Jsoup.connect(link).timeout(30000000).get();

        /*
        <tr class="provincetr">
            <td><a href="11.html">北京市<br></a></td>
        </tr>
        */
        Elements provincetrAll = document.select(".provincetr");
        for(Element provincetr : provincetrAll){
            for(Element td:provincetr.children()){
                Elements a = td.select("a");
                String href = a.attr("href");     // 11.html
                String name = a.text();           // 浙江省

                getShi(href,name);
            }
        }
    }

    // 爬取市
    private static void getShi(String shiHref,String fname) throws IOException {

        Document document = Jsoup.connect(link+shiHref).timeout(30000000).get();

        /*
        <tr class="citytr">
             <td><a href="33/3309.html">330900000000</a></td>
             <td><a href="33/3309.html">舟山市</a></td>
        </tr>
        */
        Elements citytrAll = document.select(".citytr");
        for(Element citytr:citytrAll){
            Element codetd = citytr.child(0);
            Element nametd = citytr.child(1);

            String href = codetd.select("a").attr("href");  // 33/3308.html
            String code = codetd.select("a").text();        // 330800000000
            String name = nametd.select("a").text();        // 衢州市

            BufferedWriter out = new BufferedWriter(new FileWriter("E:\\zhuijia.txt",true));
            out.write(code+" "+fname+" " +name);
            out.newLine();
            out.close();

            getQu(href,code,fname,name);
        }

    }

    // 爬取区
    private static void getQu(String countryHref,String cityCode,String fname,String cname) throws IOException {
        try {
            Document document = Jsoup.connect(link+countryHref).timeout(30000000).get();


        /*
        <tr class="countytr">
             <td><a href="02/330203.html">330203000000</a></td>
             <td><a href="02/330203.html">海曙区</a></td>
        </tr>
        */
            Elements countryAll = document.select(".countytr");

            // 这里的sort没有从0开始增加，是因为市下的第一级为市辖区，该级下无子级，用不着采集。所以直接跳过。
            long sort = -1 ;

            for(Element countrytr:countryAll){
                sort++;

                if(sort>0){
                    Element codetd = countrytr.child(0);
                    Element nametd = countrytr.child(1);

                    String href = codetd.select("a").attr("href");  // 02/330281.html
                    String code = codetd.select("a").text();        // 330281000000
                    String name = nametd.select("a").text();        // 余姚市

                    BufferedWriter out = new BufferedWriter(new FileWriter("E:\\zhuijia.txt",true));
                    out.write(code+" "+fname+" " +cname+" "+name);
                    out.newLine();
                    out.close();


                    getJiedao(href,code,fname,cname,name);
                }


            }
        }catch (Exception e){
            BufferedWriter out = new BufferedWriter(new FileWriter("E:\\error.txt",true));
            out.write(e.toString());
            out.newLine();
            out.close();
        }
    }

    // 爬取乡镇（街道）
    private static void getJiedao(String jiedaoHref,String countryCode,String fname,String cname,String qname) throws IOException {
        try {


            Document document = Jsoup.connect(link+countryCode.substring(0,2)+"/"+jiedaoHref).timeout(30000000).get();

        /*
        <tr class="towntr">
             <td><a href="12/330212001.html">330212001000</a></td>
             <td><a href="12/330212001.html">下应街道</a></td>
        </tr>
         */
            Elements townAll = document.select(".towntr");

            long sort = 0 ;

            for(Element towntr:townAll){
                Element codetd = towntr.child(0);
                Element nametd = towntr.child(1);

                String href = codetd.select("a").attr("href");  // 12/330212001.html
                String code = codetd.select("a").text();        // 330212001000
                String name = nametd.select("a").text();        // 下应街道

                BufferedWriter out = new BufferedWriter(new FileWriter("E:\\zhuijia.txt",true));
                out.write(code+" "+fname+" " +cname+" "+qname+" "+name);
                out.newLine();
                out.close();


                System.out.println(code+" "+fname+" " +cname+" "+qname+" "+name);

                //getShequ(href,code,fname,cname,qname,name);

            }
        }catch (Exception e){
            //System.out.println(e.getMessage());
            BufferedWriter out = new BufferedWriter(new FileWriter("E:\\error.txt",true));
            out.write(e.toString());
            out.newLine();
            out.close();
        }
    }

    // 爬取村（社区）
    private static void getShequ(String shequHref,String townCode,String fname,String cname,String qname,String jname) throws IOException {

        try {


            String a = townCode.substring(0,2);
            String b = townCode.substring(2,4);
            Document document = Jsoup.connect(link+a+"/"+b+"/"+shequHref).timeout(30000000).get();
         /*
        <tr class="villagetr">
             <td>330212001209</td>
             <td>112</td>
             <td>胜利村村委会</td>
        </tr>
         */
            Elements villagetrAll = document.select(".villagetr");

            long sort = 0;

            List<String> lists=new ArrayList<>();
            for(Element villagetr:villagetrAll){
                Element codetd = villagetr.child(0);
                Element nametd = villagetr.child(2);

                String code = codetd.text();        // 330212001005
                String name = nametd.text();        // 东兴社区居委会

                SysRegion region = new SysRegion();
                region.setId(Long.valueOf(code));
                region.setPid(Long.valueOf(townCode));
                region.setName(name);
                region.setTreeLevel(4);
                region.setLeaf(1);
                sort++;
                region.setSort(sort);
                //regions.add(region);
                System.out.println(code+" "+fname+" "+cname+" "+qname+" "+jname+" "+name);
                //lists.add(code+" "+fname+" "+cname+" "+qname+" "+jname+" "+name);
                BufferedWriter out = new BufferedWriter(new FileWriter("E:\\result.txt",true));
                out.write(code+" "+fname+" "+cname+" "+qname+" "+jname+" "+name);
                out.newLine();
                out.close();
            }

            //insert(lists);
        }catch (Exception e){
            BufferedWriter out = new BufferedWriter(new FileWriter("E:\\error.txt",true));
            out.write(e.toString());
            out.newLine();
            out.close();
        }
    }

    private static Connection conn=null;
    public static Connection getConnection(){
        try{
            if(conn==null){
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("class成功");
                String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
                System.out.println("url成功");
                conn= DriverManager.getConnection(url,"root","wendao");
                System.out.println("com成功");
//        		String url="jdbc:mysql://49.232.33.170:3306/cs_zyml?useUnicode=true&characterEncoding=utf-8";
//        		conn=DriverManager.getConnection(url,"root","Dfjx@1234");
            }
        }catch(Exception e){System.out.println(e.getMessage());}finally{return conn;}
    }
    static void insert(List<String> values){
        //  String sql="insert into "+"test"+" values('"+(values.replaceAll(" ","','"))+"')";
        String sql="insert into "+"test"+" values";
        for (String value:values){
            sql=sql+"('"+(value.replaceAll(" ","','"))+"'),";
        }
        sql=sql.substring(0, sql.length()-1);
        try{
//            System.out.println("sql2 =>>> "+sql);
            getConnection().createStatement().execute(sql);
        }catch(Exception e){
            e.printStackTrace();
            String id=System.currentTimeMillis()+"";
            String msg=e.getMessage().replaceAll("'","");
            String error_sql="insert into doc_item_log values('"+id+"','"+sql.replaceAll("'","")+"','"+msg+"',now())";
            try{
                System.out.println(">>>>>> 存储数据异常  >>>>>> "+e.getMessage()+" >>>>>> 归档 >>>>>> "+error_sql);
                getConnection().createStatement().execute(error_sql);
            }catch(Exception e1){e1.printStackTrace();}
        }
    }
}